// app.js
var QQMapWX = require('/utils/qqmap-wx-jssdk.min.js');
App({
	onLaunch() {
		this.handleLoginParam();
	},
	handleLoginParam(type) {
		const token = wx.getStorageSync('token')
		if (token) {
			var str = {
				token: token,
			};
			
			this.GetLocation();
			this.handleInspectToken(str);
		} else {
			this.handleLogin(type);
		}
	},
	handleLogin(type) {
		var self = this;
		wx.showModal({
			title: '温馨提示',
			content: '微信登录后才能正常使用小程序功能',
			success:(res) =>{
				if(res.confirm){
					self.handlegetUserProfile();
				}else{
					if(type == 'me'){
						wx.navigateTo({
						  url: '/pages/index/index'
						})
					}
					self.GetLocation();
					return false;
				}
			}
		});
	},
	handlegetUserProfile(){
		var self = this;
		wx.getUserProfile({
			desc: '用于完善个人资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				self.GetLocation();
				var userInfo = res.userInfo;
				wx.login({
					success: login => {
						const code = login.code;
						var str = {
							code: code,
							nickname: userInfo.nickName,
							avatar: userInfo.avatarUrl,
						}
						self.handleInspectToken(str);
					}
				})
			},
			fail:(res) =>{
				this.GetLocation();
			}
		});
		
	},
	handleInspectToken(param) {
		var self = this;
		wx.request({
			url: 'https://www.grabinter.com/User/getusertoken',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: param,
			success: getusertoken => {
				if (getusertoken.data.token) {
					var token = getusertoken.data.token;
					wx.setStorageSync("token", token);
					if(param.code){
						wx.setStorageSync("nickname", param.nickname);
						wx.setStorageSync("avatar", param.avatar);
						wx.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 3000
						});
					}
					self.globalData.hasUserInfo = true;
				} else {
					self.globalData.hasUserInfo = false;
					wx.showModal({
						title: '提示',
						content: '登录失败，确认授权重新登录？',
						success (res) {
							if (res.confirm) {
								self.handlegetUserProfile();
							} else if (res.cancel) {
								self.globalData.hasUserInfo = false;
							}
						}
					})
				}

			}
		})
	},
	GetLocation(){
		var that = this
		wx.getSetting({
			success: (res) => {
				if(res.authSetting['scope.userLocation'] == undefined){
					wx.authorize({
						scope: 'scope.userLocation',
					    success:(fres) =>{
							this.GetLocationDetail();
					    }
					})
				}else{
					if(res.authSetting['scope.userLocation']){
						this.GetLocationDetail();
					}
				}
			}
		});
		
	},
	GetLocationDetail(){
		var qqmapsdk = new QQMapWX({
			key: 'YBCBZ-WC4CU-P4EVZ-25TQK-UZAKE-XQBKR'
		});
		
		wx.getLocation({
		    type: 'wgs84',
		    success :(res) => {
				wx.setStorageSync("latitude", res.latitude);
				wx.setStorageSync("longitude", res.longitude);
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						longitude: res.longitude
					},
					success: r =>{
						var city = r.result.address_component.city;
						var province = r.result.address_component.province;
						var fprovince = '';
						var fcity = '';
						if(province.indexOf('市')>-1){
							fprovince = province.split("市").join("");
						}
						if(province.indexOf('省')>-1){
							fprovince = province.split("省").join("");
						}
						if(city.indexOf('市')>-1){
							fcity = city.split("市").join("");
						}
						this.globalData.DefaultCity = fprovince+','+fcity;
					},
					fail: function(res) {
						this.globalData.DefaultCity = 'not';
					},
				});
		        
		    },
			fail:(res) =>{
				this.globalData.DefaultCity = 'not';
			}
		})
	},
	watchDefaultCity:function(method){
		var obj = this.globalData;
		Object.defineProperty(obj,'DefaultCity', {
		      configurable: true,
		      enumerable: true,
		      set: function (value) {
		        this._DefaultCityChange = value;
		        method(value);
		      },
		      get:function(){
		        return this._DefaultCityChange
		      }
		})
	},
	watchHasUserInfo:function(method){
		var obj = this.globalData;
		Object.defineProperty(obj,'hasUserInfo', {
		      configurable: true,
		      enumerable: true,
		      set: function (value) {
		        this._HasUserInfoChange = value;
		        method(value);
		      },
		      get:function(){
		        return  this._HasUserInfoChange
		      }
		})
	},
	GetShebeiData(){
		var self = this;
		wx.request({
			url: 'https://www.grabinter.com/Works/getshebeicat',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			success: res => {
				const arr = [];
				for(var i in res.data){
					var item = res.data[i];
					var str = {
						text:item.text,
						children:[]
					}
					for(var c in item.children){
						var cstr = {
							text:item.children[c].text,
							sid:parseInt(item.children[c].sid),
						}
						str.children.push(cstr);
					}
					arr.push(str);
				}
				
				self.globalData.shebei = arr;
			}
		})
	},
	
	checkPhone(phone){
		var status = true;
		if(!(/^1[3456789]\d{9}$/.test(phone))){ 
			status = false; 
		}
		return status;
	},
	
	onShow: function() {
		this.GetShebeiData();
	},
	globalData: {
		hasUserInfo: false,
		userInfo: {},
		DefaultCity:'',
		shebei:[],
		jobskills:[
			{id:1,text:'矿山活',checked:false},
			{id:2,text:'土方活',checked:false},
			{id:3,text:'隧道活',checked:false},
			{id:4,text:'市政活',checked:false},
			{id:5,text:'修路活',checked:false},
			{id:6,text:'煤矿活',checked:false},
			{id:7,text:'农田改造',checked:false},
			{id:8,text:'河道活',checked:false},
			{id:9,text:'拆楼拆迁',checked:false},
			{id:10,text:'房地产活',checked:false},
			{id:11,text:'水利活',checked:false},
			{id:12,text:'其它',checked:false},
		],
	}
})
